package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.RoleInf;
import cn.edu.cdu.group4nb.po.UserInf;
import cn.edu.cdu.group4nb.service.RoleService;
import cn.edu.cdu.group4nb.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @auther: tupeng
 * @date: 2019-10-09 11:13
 */
@Controller
@RequestMapping("/roleManager")
public class RoleController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public R roleManagerAdd(
            @RequestBody Map<String, Object> map
//            @RequestBody String roledetail
//            HttpServletRequest request
//                            @RequestParam("rolename") String rolename,
//                            @RequestParam("roledetail") String roledetail
    ) {

        String rolename = (String) map.get("rolename");
        String roledetail = (String) map.get("roledetail");
        System.out.println(rolename);
        System.out.println(roledetail);
        try {
            roleService.addRole(rolename, roledetail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public R roleManagerDelete(@RequestBody int[] roleid) {
//        ArrayList<String> list = new ArrayList<String>(Arrays.asList(roleid)) ;
        try {
            roleService.deleteRole(roleid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<RoleInf> roleManagerQuery() {
        try {
            List<RoleInf> roleInfs;
            roleInfs = roleService.queryAllRole();
            for (RoleInf roleInf : roleInfs) {
                System.out.println(roleInf.getRoleid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(roleService.queryAllRole());
        return roleService.queryAllRole();
    }

    @RequestMapping("/queryRoleById")
    @ResponseBody
    public R roleManagerQueryRoleById(@RequestBody int roleid) {
        System.out.println(roleService.queryRoleById(roleid));
        return R.ok().put("roleInf", roleService.queryRoleById(roleid));
    }

    @RequestMapping("/update")
    @ResponseBody
    public R roleManagerUpdate(
//            @RequestParam("rolename") String rolename,
//            @RequestParam("roledetail") String roledetail,
//            @RequestParam("roleid") String userid
              @RequestBody Map<String,Object> roles ) {
        String rolename= (String) roles.get("rolename");
        String roledetail= (String) roles.get("roledetail");
        int roleid1= (int) roles.get("roleid");
        String roleid=String.valueOf(roleid1);
//        int roleid=Integer.parseInt(roleid1);
        try {
            roleService.updateRole(rolename, roledetail, roleid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

}
